/*
 * @Descripttion : 全局变量
 * @Versions     : 0.1
 * @Author       : foxui team
 * @Date         : 2022-03-08 22:23:44
 * @LastEditors  : foxui team
 * @LastEditTime : 2022-03-13 20:37:48
 */

import { _randomInt } from './utils';

let _g = {
    /* zIndex
    --------------------------------------- */
    zIndex: 2021, // 弹出框层级

    /* loading
    --------------------------------------- */
    LOADINGSIZE: 38, // 加载图片的默认大小
    LOADINGCOLOR: '#2d8cf0', //加载图片的默认颜色

    /* foxId
    --------------------------------------- */
    FOXIDMAX: 9999, // 最大值
    FOXIDMIN: 1000, // 最小值
    foxIdList: [], // 保存已存在的Id
};

/**
 * @description: 生成 foxId
 * @param {*} max
 * @param {*} min
 * @return {*}
 * @Date: 2022-03-09 09:19:31
 */
function _foxId(max, min) {
    max = max || _g.FOXIDMAX;
    min = min || _g.FOXIDMIN;
    let randomNum = _randomInt(max, min);
    while (_g.foxIdList.indexOf(randomNum) !== -1) {
        randomNum = _randomInt(max, min);
    }
    _g.foxIdList.push(randomNum);
    return randomNum;
}

/**
 * @description: 获取 zIndex (每获取一次，就会自增 1)
 * @param {*}
 * @return {*}
 * @Date: 2022-03-09 09:20:50
 */
function _zIndex() {
    _g.zIndex++;
    return _g.zIndex;
}

/**
 * @description: 加载图标
 * @param {*} loadingSize
 * @param {*} loadingColor
 * @param {*} type              类型：circular, spinner；可传可不传，不传默认为 circular
 * @return {*}
 * @Date: 2022-03-09 11:31:20
 */
function loadingIcon(data) {
    let size = data && data.size ? data.size : _g.LOADINGSIZE,
        color = data && data.color ? data.color : _g.LOADINGCOLOR,
        type = data && data.type ? data.type : '';

    let circular = [
        `<svg width="${size}" height="${size}" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg" stroke="${color}">`,
        '<g fill="none" fill-rule="evenodd">',
        '<g transform="translate(1 1)" stroke-width="2">',
        '<circle stroke-opacity=".5" cx="18" cy="18" r="18"/>',
        '<path d="M36 18c0-9.94-8.06-18-18-18">',
        '<animateTransform attributeName="transform" type="rotate" from="0 18 18" to="360 18 18" dur="1s" repeatCount="indefinite"/>',
        '</path>',
        '</g>',
        '</g>',
        '</svg>',
    ];

    let spinner = [
        `<svg width="${size}" height="${size}" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg" fill="${color}">`,
        '<path d="M19,0c0.5,0,0.9,0.2,1.2,0.5c0.3,0.3,0.5,0.8,0.5,1.2v5.2c0,1-0.8,1.7-1.7,1.7c-1,0-1.7-0.8-1.7-1.7V1.7C17.3,0.8,18,0,19,0L19,0z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="1; 0.917; 0.833; 0.75; 0.667; 0.583; 0.5; 0.416; 0.333; 0.25; 0.167; 0.083" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M28.5,2.5c0.8,0.5,1.1,1.5,0.6,2.4l-2.6,4.5c-0.3,0.5-0.9,0.9-1.5,0.9c-0.6,0-1.2-0.3-1.5-0.9c-0.3-0.5-0.3-1.2,0-1.7l2.6-4.5C26.6,2.4,27.7,2.1,28.5,2.5L28.5,2.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.083; 1; 0.917; 0.833; 0.75; 0.667; 0.583; 0.5; 0.416; 0.333; 0.25; 0.167" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M35.5,9.5c0.5,0.8,0.2,1.9-0.6,2.4l-4.5,2.6c-0.8,0.5-1.9,0.2-2.3-0.6c-0.5-0.8-0.2-1.9,0.6-2.4l4.5-2.6C33.9,8.4,35,8.7,35.5,9.5L35.5,9.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.167; 0.083; 1; 0.917; 0.833; 0.75; 0.667; 0.583; 0.5; 0.416; 0.333; 0.25" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M38,19c0,0.5-0.2,0.9-0.5,1.2c-0.3,0.3-0.8,0.5-1.2,0.5h-5.2c-1,0-1.7-0.8-1.7-1.7c0-1,0.8-1.7,1.7-1.7h5.2c0.5,0,0.9,0.2,1.2,0.5C37.8,18.1,38,18.5,38,19L38,19z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.25; 0.167; 0.083; 1; 0.917; 0.833; 0.75; 0.667; 0.583; 0.5; 0.416; 0.333" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M35.5,28.5c-0.5,0.8-1.5,1.1-2.4,0.6l-4.5-2.6c-0.5-0.3-0.9-0.9-0.9-1.5c0-0.6,0.3-1.2,0.9-1.5c0.5-0.3,1.2-0.3,1.7,0l4.5,2.6C35.6,26.6,35.9,27.7,35.5,28.5L35.5,28.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.333; 0.25; 0.167; 0.083; 1; 0.917; 0.833; 0.75; 0.667; 0.583; 0.5; 0.416" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M28.5,35.5c-0.8,0.5-1.9,0.2-2.4-0.6l-2.6-4.5c-0.5-0.8-0.2-1.9,0.6-2.3c0.8-0.5,1.9-0.2,2.4,0.6l2.6,4.5C29.6,33.9,29.3,35,28.5,35.5L28.5,35.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.416; 0.333; 0.25; 0.167; 0.083; 1; 0.917; 0.833; 0.75; 0.667; 0.583; 0.5" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M19,38c-0.5,0-0.9-0.2-1.2-0.5c-0.3-0.3-0.5-0.8-0.5-1.2v-5.2c0-1,0.8-1.7,1.7-1.7c1,0,1.7,0.8,1.7,1.7v5.2c0,0.5-0.2,0.9-0.5,1.2C19.9,37.8,19.5,38,19,38L19,38z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.5; 0.416; 0.333; 0.25; 0.167; 0.083; 1; 0.917; 0.833; 0.75; 0.667; 0.583" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M9.5,35.5c-0.8-0.5-1.1-1.5-0.6-2.4l2.6-4.5c0.3-0.5,0.9-0.9,1.5-0.9c0.6,0,1.2,0.3,1.5,0.9c0.3,0.5,0.3,1.2,0,1.7l-2.6,4.5C11.4,35.6,10.3,35.9,9.5,35.5L9.5,35.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.583; 0.5; 0.416; 0.333; 0.25; 0.167; 0.083; 1; 0.917; 0.833; 0.75; 0.667" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M2.5,28.5c-0.5-0.8-0.2-1.9,0.6-2.4l4.5-2.6c0.8-0.5,1.9-0.2,2.3,0.6c0.5,0.8,0.2,1.9-0.6,2.4l-4.5,2.6C4.1,29.6,3,29.3,2.5,28.5L2.5,28.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.667; 0.583; 0.5; 0.416; 0.333; 0.25; 0.167; 0.083; 1; 0.917; 0.833; 0.75" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M0,19c0-1,0.8-1.7,1.7-1.7h5.2c1,0,1.7,0.8,1.7,1.7c0,1-0.8,1.7-1.7,1.7H1.7c-0.5,0-0.9-0.2-1.2-0.5C0.2,19.9,0,19.5,0,19L0,19z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.75; 0.667; 0.583; 0.5; 0.416; 0.333; 0.25; 0.167; 0.083; 1; 0.917; 0.833" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M2.5,9.5C3,8.7,4.1,8.4,4.9,8.9l4.5,2.6c0.5,0.3,0.9,0.9,0.9,1.5c0,0.6-0.3,1.2-0.9,1.5c-0.5,0.3-1.2,0.3-1.7,0l-4.5-2.6C2.4,11.4,2.1,10.3,2.5,9.5L2.5,9.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.833; 0.75; 0.667; 0.583; 0.5; 0.416; 0.333; 0.25; 0.167; 0.083; 1; 0.917" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '<path d="M9.5,2.5c0.8-0.5,1.9-0.2,2.4,0.6l2.6,4.5c0.5,0.8,0.2,1.9-0.6,2.3c-0.8,0.5-1.9,0.2-2.4-0.6L8.9,4.9C8.4,4.1,8.7,3,9.5,2.5L9.5,2.5z">',
        '<animate attributeName="opacity" begin="0s" dur="1.3s" values="0.917; 0.833; 0.75; 0.667; 0.583; 0.5; 0.416; 0.333; 0.25; 0.167; 0.083; 1" calcMode="linear" repeatCount="indefinite" />',
        '</path>',
        '</svg>',
    ];

    return type === 'spinner' ? spinner.join('') : circular.join('');
}

export { _foxId, _zIndex, loadingIcon };
